package com.ruoyi.ctcx.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.ctcx.domain.TbInsuranceSettle;
import com.ruoyi.ctcx.domain.TbInsuranceSettleCasePay;
import com.ruoyi.ctcx.domain.vo.TbInsuranceSettleStatics;
import com.ruoyi.ctcx.service.ITbInsuranceSettleCasePayService;
import com.ruoyi.ctcx.service.ITbInsuranceSettleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ctcx.domain.TbInsuranceSettleCase;
import com.ruoyi.ctcx.service.ITbInsuranceSettleCaseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 保险理赔赔案信息Controller
 * 
 * @author ruoyi
 * @date 2024-11-20
 */
@RestController
@RequestMapping("/insurance/case")
public class TbInsuranceSettleCaseController extends BaseController
{
    @Autowired
    private ITbInsuranceSettleCaseService tbInsuranceSettleCaseService;

    @Autowired
    private ITbInsuranceSettleService tbInsuranceSettleService;

    @Autowired
    private ITbInsuranceSettleCasePayService tbInsuranceSettleCasePayService;

    /**
     * 查询保险理赔赔案信息列表
     */
    @PreAuthorize("@ss.hasPermi('insurance:case:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbInsuranceSettleCase tbInsuranceSettleCase)
    {
        startPage();
        List<TbInsuranceSettleCase> list = tbInsuranceSettleCaseService.selectTbInsuranceSettleCaseList(tbInsuranceSettleCase);
        return getDataTable(list);
    }

    /**
     * 导出保险理赔赔案信息列表
     */
    @PreAuthorize("@ss.hasPermi('insurance:case:export')")
    @Log(title = "保险理赔赔案信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbInsuranceSettleCase tbInsuranceSettleCase)
    {
        List<TbInsuranceSettleCase> list = tbInsuranceSettleCaseService.selectTbInsuranceSettleCaseList(tbInsuranceSettleCase);
        ExcelUtil<TbInsuranceSettleCase> util = new ExcelUtil<TbInsuranceSettleCase>(TbInsuranceSettleCase.class);
        util.exportExcel(response, list, "保险理赔赔案信息数据");
    }

    /**
     * 获取保险理赔赔案信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('insurance:case:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbInsuranceSettleCaseService.selectTbInsuranceSettleCaseById(id));
    }

    /**
     * 新增保险理赔赔案信息
     */
    @PreAuthorize("@ss.hasPermi('insurance:case:add')")
    @Log(title = "保险理赔赔案信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbInsuranceSettleCase tbInsuranceSettleCase)
    {


        String settleId = tbInsuranceSettleCase.getSettleId();
        if (null == settleId ||"".equals(settleId))
        {
            settleId = IdUtils.simpleUUID();
            tbInsuranceSettleCase.setSettleId(settleId);
        }

        //保存赔案信息
        tbInsuranceSettleCase.setCreateBy(getUsername());
        tbInsuranceSettleCaseService.insertTbInsuranceSettleCase(tbInsuranceSettleCase);

        //修改理赔信息
        updateSettle(tbInsuranceSettleCase,settleId);

        return toAjax(1);
    }

    /**
     * 修改保险理赔赔案信息
     */
    @PreAuthorize("@ss.hasPermi('insurance:case:edit')")
    @Log(title = "保险理赔赔案信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbInsuranceSettleCase tbInsuranceSettleCase)
    {
        //保存赔案信息
        tbInsuranceSettleCase.setUpdateBy(getUsername());
        tbInsuranceSettleCaseService.updateTbInsuranceSettleCase(tbInsuranceSettleCase);

        //修改理赔信息
        updateSettle(tbInsuranceSettleCase,"");

        return toAjax(1);
    }


    /**
     * 删除保险理赔赔案信息
     */
    @PreAuthorize("@ss.hasPermi('insurance:case:remove')")
    @Log(title = "保险理赔赔案信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {

        for (Long id : ids)
        {
            TbInsuranceSettleCasePay query = new TbInsuranceSettleCasePay();

            query.setCaseId(id);

            List<TbInsuranceSettleCasePay> payList = tbInsuranceSettleCasePayService.selectTbInsuranceSettleCasePayList(query);

            if (null != payList && !payList.isEmpty())
            {
                return error("赔案信息下存在支付信息，无法删除");
            }

        }

        //更新理赔信息
        for (Long id : ids)
        {
            TbInsuranceSettleCase tbInsuranceSettleCase = tbInsuranceSettleCaseService.selectTbInsuranceSettleCaseById(id);

            //删除保险理赔赔案信息
            tbInsuranceSettleCaseService.deleteTbInsuranceSettleCaseById(id);

            //修改理赔信息
            updateSettle(tbInsuranceSettleCase,"");

        }
        return toAjax(1);
    }


    /**
     * 更新理赔信息
     */
    private void updateSettle(TbInsuranceSettleCase tbInsuranceSettleCase,String settleId)
    {
        //修改理赔信息
        TbInsuranceSettle tbInsuranceSettle = tbInsuranceSettleService.selectTbInsuranceSettleById(tbInsuranceSettleCase.getSettleId());


        TbInsuranceSettleStatics settle = tbInsuranceSettleCaseService.getCaseStaticsBySettleId(tbInsuranceSettleCase.getSettleId());

        //新增
        if(null == tbInsuranceSettle)
        {
            tbInsuranceSettle = new TbInsuranceSettle();
            tbInsuranceSettle.setCreateBy(getUsername());
            tbInsuranceSettle.setId(settleId);
            tbInsuranceSettle.setInsureId(tbInsuranceSettleCase.getInsureId());
            if (null != settle)
            {
                tbInsuranceSettle.setSettleNum(settle.getSettleNum());
                tbInsuranceSettle.setCompleteNum(settle.getCompleteNum());
                tbInsuranceSettle.setTotalAmount(settle.getTotalAmount());
            }
            tbInsuranceSettleService.insertTbInsuranceSettle(tbInsuranceSettle);

        }else {

            if (null != settle)
            {
                tbInsuranceSettle.setSettleNum(settle.getSettleNum());
                tbInsuranceSettle.setCompleteNum(settle.getCompleteNum());
                tbInsuranceSettle.setTotalAmount(settle.getTotalAmount());
                tbInsuranceSettle.setUpdateBy(getUsername());
            }

            //保存理赔信息
            tbInsuranceSettleService.updateTbInsuranceSettle(tbInsuranceSettle);
        }

    }

    /**
     * 新增赔案信息
     */
    @PreAuthorize("@ss.hasPermi('insurance:case:add')")
    @Log(title = "新增赔案信息", businessType = BusinessType.INSERT)
    @PostMapping("/saveCase")
    public AjaxResult saveCase(@RequestBody TbInsuranceSettleCase tbInsuranceSettleCase)
    {

        TbInsuranceSettle tbInsuranceSettle = new TbInsuranceSettle();
        //保存理赔信息
        if (null == tbInsuranceSettleCase.getSettleId())
        {
            //新增理赔信息
            String settleId = IdUtils.simpleUUID();
            tbInsuranceSettle.setId(settleId);
            tbInsuranceSettle.setInsureId(tbInsuranceSettleCase.getInsureId());
            tbInsuranceSettle.setSettleNum(Long.valueOf(1));
            //已结案
            if ("2".equals(tbInsuranceSettleCase.getCaseStatus()))
            {
                tbInsuranceSettle.setCompleteNum(Long.valueOf(1));
            }
            //未结案
            else
            {
                tbInsuranceSettle.setCompleteNum(Long.valueOf(0));
            }
            tbInsuranceSettle.setTotalAmount(tbInsuranceSettleCase.getCaseAmount());//报损金额

            //新增理赔信息
            tbInsuranceSettleService.insertTbInsuranceSettle(tbInsuranceSettle);
            tbInsuranceSettleCase.setSettleId(settleId);
        }
        else {

            //修改理赔信息
            tbInsuranceSettle = tbInsuranceSettleService.selectTbInsuranceSettleById(tbInsuranceSettleCase.getSettleId());

            Long settleNum = tbInsuranceSettle.getSettleNum();
            tbInsuranceSettle.setSettleNum(settleNum+1);

            //已结案
            if ("2".equals(tbInsuranceSettleCase.getCaseStatus()))
            {
                tbInsuranceSettle.setCompleteNum(tbInsuranceSettle.getCompleteNum()+1);
            }

            //报损金额
            if (null != tbInsuranceSettleCase.getCaseAmount())
            {
                tbInsuranceSettle.setTotalAmount(tbInsuranceSettle.getTotalAmount().add(tbInsuranceSettleCase.getCaseAmount()));
            }

            //修改理赔信息
            tbInsuranceSettleService.updateTbInsuranceSettle(tbInsuranceSettle);
        }

        //保存赔案信息
        return toAjax(tbInsuranceSettleCaseService.insertTbInsuranceSettleCase(tbInsuranceSettleCase));
    }



    /**
     * 修改赔案信息
     */
    @PreAuthorize("@ss.hasPermi('insurance:case:edit')")
    @Log(title = "修改赔案信息", businessType = BusinessType.INSERT)
    @PostMapping("/updateCase")
    public AjaxResult updateCase(@RequestBody TbInsuranceSettleCase tbInsuranceSettleCase)
    {


        //保存赔案信息
        tbInsuranceSettleCaseService.updateTbInsuranceSettleCase(tbInsuranceSettleCase);

        //修改理赔信息
        updateSettle(tbInsuranceSettleCase,"");

        return toAjax(0);
    }
}
